import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class StatisticsRecord {
    public int count;
    public int over1000;
    public int over10000;
    public int over100000;
    public int max;
    public int sum;

    /**
     * 初始化方法
     */
    public StatisticsRecord() {
        count = 0;
        over1000 = 0;
        over10000 = 0;
        over100000 = 0;
        max = 0;
        sum = 0;
    }

    /**
     * 添加一个视频的播放量
     *
     * @param view 播放量
     */
    public void add(int view) {
        count++;
        if (view >= 1000) {
            over1000++;
        }
        if (view >= 10000) {
            over10000++;
        }
        if (view >= 100000) {
            over10000++;
        }
        max = Math.max(max, view);
        sum += view;
    }

    public void insertIntoDB(Connection connection, String month, String type) throws SQLException {
        String sql = "INSERT INTO table_statistics (`month`, `type`, `count`, `sum`, `max`, `over100000`, `over10000`, `over1000`) VALUES (?,?,?,?,?,?,?,?);";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, month);
        preparedStatement.setString(2, type);
        preparedStatement.setInt(3, count);
        preparedStatement.setInt(4, sum);
        preparedStatement.setInt(5, max);
        preparedStatement.setInt(6, over100000);
        preparedStatement.setInt(7, over10000);
        preparedStatement.setInt(8, over1000);
        preparedStatement.executeUpdate();
        preparedStatement.close();
    }
}
